package net.wofly.sale.commodity.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.condition.OrderCondition;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.GeneralHelper;
import net.wofly.common.util.Identities;
import net.wofly.common.util.PageHelper;
import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.domain.CommodityTerminal;
import net.wofly.sale.commodity.domain.CommodityTerminalView;
import net.wofly.sale.commodity.repository.CommodityTerminalViewRepository;
import net.wofly.sale.commodity.repository.CommodityTmlRepository;
import net.wofly.sale.commodity.service.ICommodityService;
import net.wofly.sale.commodity.service.ICommodityTmlService;
import net.wofly.sale.commodity.util.CommodityTerminalType;

@Service("iCommodityTmlService")
@Transactional  
public class CommodityTmlServiceImpl implements ICommodityTmlService {
    
    @Autowired
    CommodityTmlRepository commodityTmlRepository;
    @Autowired
    ICommodityService iCommodityService ;
    
    @Autowired
	CommodityTerminalViewRepository commodityTerminalViewRepository ;
    
    @Override
    public CommodityTerminal findOne(String commodityTerminalID) {
        return commodityTmlRepository.findOne(commodityTerminalID);
    }
    
    @Override
    public List<CommodityTerminal> findAll(List<String> commodityTerminalIDs) {
        return commodityTmlRepository.findAll(commodityTerminalIDs);
    }
    
    @Override
    public CommodityTerminal findByCommodityIDAndCommodityTerminalTypeAndStatus(
            String commodityID, Integer commodityTerminalType, Integer status) {
        return commodityTmlRepository.findByCommodityIDAndCommodityTerminalTypeAndStatus(commodityID,
                commodityTerminalType, status);
    }
    
    @Override
    public void saveByCommodity(Commodity commodity) {
    		List<CommodityTerminal> commodityTerminals = commodity.getCommodityTerminalList() ;
    		
    		if(null == commodityTerminals) {
    			this.saveByEmptyCommodityTerminal(commodity);
    			return ;
    		}
    	
    		commodityTmlRepository.save(commodityTerminals);
    }
    
    private void saveByEmptyCommodityTerminal(Commodity commodity) {
    		List<CommodityTerminal> commodityTerminals = new ArrayList<>(4);
        commodityTerminals.add(initCommodityTerminal(commodity, CommodityTerminalType.会员.getValue()));
        commodityTerminals.add(initCommodityTerminal(commodity, CommodityTerminalType.流通.getValue()));
        commodityTerminals.add(initCommodityTerminal(commodity, CommodityTerminalType.酒店暗标款.getValue()));
        commodityTerminals.add(initCommodityTerminal(commodity, CommodityTerminalType.酒店.getValue()));
        commodityTmlRepository.save(commodityTerminals);
    }

    private CommodityTerminal initCommodityTerminal(Commodity commodity ,Integer commodityTerminalType){
        CommodityTerminal commodityTerminal = new CommodityTerminal();
        commodityTerminal.setCommodityID(commodity.getCommodityID());
        commodityTerminal.setCommodityTerminalID(Identities.uuid2());
        commodityTerminal.setCommodityTerminalType(commodityTerminalType);
        commodityTerminal.setCommodityTerminalName(commodity.getCommodityName() + "-" + CommodityTerminalType.getName(commodityTerminalType));
        commodityTerminal.setCommodityTerminalCode("JLT" + commodityTerminalType + commodity.getCommodityCode());
        commodityTerminal.setModifyDate(System.currentTimeMillis());
        commodityTerminal.setStatus(1);
        return commodityTerminal;
    }

	@Override
	public PageWrapper<CommodityTerminal> findAllCommodityTerminalByStatusAndHasCommodityTerminal(String commodityTerminalName, Integer status ,Integer hasCommodityTerminal,Integer pageNum, Integer pageSize){
		Condition condition = new Condition();
        List<FilterCondition> filterConditionList = new ArrayList<>();
        List<OrderCondition> orderConditions = new ArrayList<>();
        if (StringUtils.isNotBlank(commodityTerminalName)) {
        		if (GeneralHelper.isChinese(commodityTerminalName)) {
        			filterConditionList.add(new FilterCondition(commodityTerminalName, "commodityTerminalName", ConditionRelate.and.name(), Operation.like.name(), 1));
        		}else {
        			filterConditionList.add(new FilterCondition(commodityTerminalName, "commodityTerminalNamePinYin", ConditionRelate.and.name(), Operation.like.name(), 1));
        		}
        }
        if (status != null) {
            filterConditionList.add(new FilterCondition(status.toString(), "status", ConditionRelate.and.name(), Operation.eq.name(), 2));
        }
        if (hasCommodityTerminal != null) {
            filterConditionList.add(new FilterCondition(String.valueOf(hasCommodityTerminal), "hasCommodityTerminal", ConditionRelate.and.name(), Operation.eq.name(), 3));
        }
        condition.setFilterCondition(filterConditionList);
        
        condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), CommodityTerminal.class).getResult();

        List<Order> orders=new ArrayList<>();
		orders.add( new Order(Direction.DESC, "commodityID"));
		orders.add( new Order(Direction.ASC, "commodityTerminalType"));
		Sort sort = new Sort(orders);
        Pageable pageable = new PageRequest(pageNum - 1, pageSize, sort);
        Page<CommodityTerminal> page = commodityTmlRepository.findAll(condition.getSpecification(), pageable);
		
		List<CommodityTerminal> commodityTerminals = page.getContent() ;
		for(CommodityTerminal commodityTerminal:commodityTerminals) {
			commodityTerminal.setCommodity(iCommodityService.findOne(commodityTerminal.getCommodityID()));
		}
		
		return PageHelper.toPage(page)  ;
	}
	
	@Override
	public Commodity findCommodityByCommodityTerminalID(String commodityTerminalID) {
		return commodityTmlRepository.findCommodityByCommodityTerminalID(commodityTerminalID) ;
	}

	@Override
	public List<CommodityTerminal> findByCommodityID(String commodityID) {
		List<CommodityTerminal> commodityTerminals = commodityTmlRepository.findByCommodityID(commodityID) ;
		
		return commodityTerminals ;
	}

	@Override
	public void delByCommodityID(String commodityID) {
		List<CommodityTerminal> commodityTerminals = this.findByCommodityID(commodityID) ;
		
		for(CommodityTerminal commodityTerminal:commodityTerminals) {
			commodityTerminal.setStatus(0);
		}
		
		this.commodityTmlRepository.save(commodityTerminals) ;
	}

	@Override
	public void updateCommodityTerminal(Commodity commodity) {
		if (null != commodity.getCommodityTerminalList()) {
			this.commodityTmlRepository.save(commodity.getCommodityTerminalList()) ;
			return ;
		}
		
		List<CommodityTerminal> commodityTerminals = this.findByCommodityID(commodity.getCommodityID()) ;
		
		for(CommodityTerminal commodityTerminal:commodityTerminals) {
			commodityTerminal.setCommodityTerminalCode(commodity.getCommodityCode());
			commodityTerminal.setCommodityTerminalName(commodity.getCommodityName() + "-" + commodityTerminal.getCommodityTerminalName().split("-")[1] );
		}
		
		this.commodityTmlRepository.save(commodityTerminals) ;
		
	}

	@Override
	public PageWrapper<CommodityTerminalView> findAllCommodityTerminalViewByPage(String commodityTerminalName,
			String commodityTypeID, Integer status, Integer hasCommodityTerminal, Integer pageNum, Integer pageSize) {
		
		Condition condition = new Condition();
        List<FilterCondition> filterConditionList = new ArrayList<>();
        List<OrderCondition> orderConditions = new ArrayList<>();
        if (StringUtils.isNotBlank(commodityTerminalName)) {
        		if (GeneralHelper.isChinese(commodityTerminalName)) {
        			filterConditionList.add(new FilterCondition(commodityTerminalName, "commodityTerminalName", ConditionRelate.and.name(), Operation.like.name(), 1));
        		}else {
        			filterConditionList.add(new FilterCondition(commodityTerminalName, "commodityTerminalNamePinYin", ConditionRelate.and.name(), Operation.like.name(), 1));
        		}
        }
        
        if (commodityTypeID != null) {
            filterConditionList.add(new FilterCondition(commodityTypeID, "commodityTypeID", ConditionRelate.and.name(), Operation.eq.name(), 2));
        }
        
        if (status != null) {
            filterConditionList.add(new FilterCondition(status.toString(), "status", ConditionRelate.and.name(), Operation.eq.name(), 2));
        }
        if (hasCommodityTerminal != null) {
            filterConditionList.add(new FilterCondition(String.valueOf(hasCommodityTerminal), "hasCommodityTerminal", ConditionRelate.and.name(), Operation.eq.name(), 3));
        }
        condition.setFilterCondition(filterConditionList);
        
        condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), CommodityTerminalView.class).getResult();

        List<Order> orders=new ArrayList<>();
		orders.add( new Order(Direction.DESC, "commodityID"));
		orders.add( new Order(Direction.ASC, "commodityTerminalType"));
		Sort sort = new Sort(orders);
        Pageable pageable = new PageRequest(pageNum - 1, pageSize, sort);
        Page<CommodityTerminalView> page = commodityTerminalViewRepository.findAll(condition.getSpecification(), pageable);
        List<CommodityTerminalView> commodityTerminalViews = page.getContent() ;
        
        for(CommodityTerminalView commodityTerminalView:commodityTerminalViews) {
        		commodityTerminalView.setCommodity(iCommodityService.findOne(commodityTerminalView.getCommodityID()));
		}
        
		return PageHelper.toPage(page)  ;
	}

}
